﻿@model IEnumerable<CmsEntityMetaField>
           
@{
    Layout = "~/admin/Templates/TelerikAdministration.cshtml";
}

@{
    ViewBag.Title = "Meta fields";
}

<h1 class="g-clear">
	<i class="b-icon b-icon_h b-icon_h_config"></i>
	<span class="b-overflow">
	Common meta fields
	<span class="b-intro-text">This section allows to manage various site properties, usually referenced in the site source code. Please discuss with developers before changing anything.</span> 
	</span>  
</h1>

<p class="clear-left">
    <a class="b-button" href="@Url.Action("add", "cmsmetafield")">Add new meta field</a>
</p>
@{Html.RenderPartial("ErrorPanel");}
@(Html.Telerik().Grid(Model).Name("MetaFieldGrid")
        .Columns(columns =>
        {
            columns.Bound(p => p.MetaFieldName).Template(
                @<text>
                <a href="@Url.Action("edit", "cmsmetafield", new { id = item.Id })">@item.MetaFieldName</a>
                </text>
            ).ClientTemplate("<a href=" + Url.Action("edit", "cmsmetafield") + "/<#= Id#>><#= MetaFieldName#></a>");
            columns.Bound(p => p.MetaFieldDescription);
            columns.Bound(p => p.Id);
            columns.Template(
                @<text>
                <a class="b-icon b-icon_delete" title="Delete" href="@Url.Action("delete", "cmsmetafield", new { id = item.Id })">Delete</a>
                </text>
            )
            .ClientTemplate("<a id='metafield-delete-<#= Id #>' class='b-icon b-icon_delete' title='Delete'>Delete</a>")
            .Title("Delete");
        })
        .DataBinding(dataBinding =>
        {
            dataBinding.Ajax().Select("_Index", "CmsMetaField");
        })
        .ClientEvents(events => events.OnRowDataBound("grid_onRowDataBound").OnError("grid_onError"))
        .Scrollable(scrolling => scrolling.Enabled(false))
        .Sortable(sorting => sorting.Enabled(true))
        //.Pageable(paging => paging.PageSize((int)ViewBag.PageSize).Enabled(Model.Count() > (int)ViewData.PageSize))
)
@{Html.RenderPartial("GridSizer", (int)ViewBag.PageSize);}

@section scriptBlock{
<script type="text/javascript">
// <![CDATA[
    $(document).ready(function () {
        $(".b-icon_delete").live("click", function () {
            if (confirm("Are you sure you want to delete this meta field?")) {
                var urlParts = $(this).attr('href').split('/');
                var url = '@Url.Action("deleteajax", "cmsmetafield")';
                $.ajax({
                    type: 'DELETE',
                    url: url + '/' + urlParts[urlParts.length - 1],
                    error: function (e) {
                        viewEventModel.addEvent({ eventBody: e.responseText });
                    },
                    success: function (data) {
                        if (data == 1) {
                            $(".t-refresh").click();
                        }
                    },
                    dataType: 'json'
                });
            }
            return false;
        });
    });
    function grid_onRowDataBound(e) {
        var dataItem = e.dataItem
        var $el = $('#metafield-delete-' + dataItem.Id);
        if ($el) {
            if (!dataItem.IsSystem) {
                $el.attr('href', '@Url.Action("delete", "cmsmetafield")' + "/" + dataItem.Id);
            }
            else {
                $el.hide().remove();
            }
        }
    }
    // ]]>
</script>
}